﻿using System.Linq;
using UnityEditor;
using UnityEngine;
using GFUtility = GameFramework.Utility;
namespace LS.Editor.DataTableTools
{
    public sealed class DataTableGeneratorMenu
    {
        [MenuItem("Lancer Studio/Builting/Generate DataTables CodeFile")]
        private static void GenerateDataTablesCodeFile()
        {
            string assetGUID = AssetDatabase.FindAssets("t:DataTableGeneratingCollection").Single();
            string path = AssetDatabase.GUIDToAssetPath(assetGUID);
            DataTableGeneratingCollection collection = AssetDatabase.LoadAssetAtPath<DataTableGeneratingCollection>(path);
            for (int i = 0; i < collection.Collection.Count; i++)
            {
                var fileInfo = collection.Collection[i];
                if (!fileInfo.EnableGenerate)
                {
                    continue;
                }

                string dataTableName = fileInfo.Name;
                DataTableProcessor dataTableProcessor = DataTableGenerator.CreateDataTableProcessor(collection.Collection[i].FilePath);
                if (!DataTableGenerator.CheckRawData(dataTableProcessor, dataTableName))
                {
                    Debug.LogError(GFUtility.Text.Format("Check raw data failure. DataTableName='{0}'", dataTableName));
                    continue;
                }

                DataTableGenerator.GenerateCodeFile(dataTableProcessor, dataTableName);
            }
            
            AssetDatabase.Refresh();
        }

        [MenuItem("Lancer Studio/Builting/Generate DataTables Binary File")]
        private static void GenerateDataTablesBinaryFile()
        {
            string assetGUID = AssetDatabase.FindAssets("t:DataTableGeneratingCollection").Single();
            string path = AssetDatabase.GUIDToAssetPath(assetGUID);
            DataTableGeneratingCollection collection = AssetDatabase.LoadAssetAtPath<DataTableGeneratingCollection>(path);
            for (int i = 0; i < collection.Collection.Count; i++)
            {
                var fileInfo = collection.Collection[i];
                string dataTableName = fileInfo.Name;
                DataTableProcessor dataTableProcessor = DataTableGenerator.CreateDataTableProcessor(collection.Collection[i].FilePath);
                if (!DataTableGenerator.CheckRawData(dataTableProcessor, dataTableName))
                {
                    Debug.LogError(GFUtility.Text.Format("Check raw data failure. DataTableName='{0}'", dataTableName));
                    continue;
                }

                DataTableGenerator.GenerateDataFile(dataTableProcessor, dataTableName);
            }

            AssetDatabase.Refresh();
        }

        private static void GenerateDataTables(bool genBytesFile = false)
        {
            string assetGUID = AssetDatabase.FindAssets("t:DataTableGeneratingCollection").Single();
            string path = AssetDatabase.GUIDToAssetPath(assetGUID);
            DataTableGeneratingCollection collection = AssetDatabase.LoadAssetAtPath<DataTableGeneratingCollection>(path);
            for (int i = 0; i < collection.Collection.Count; i++)
            {
                var fileInfo = collection.Collection[i];
                if (!fileInfo.EnableGenerate)
                {
                    continue;
                }

                string dataTableName = fileInfo.Name;
                DataTableProcessor dataTableProcessor = DataTableGenerator.CreateDataTableProcessor(collection.Collection[i].FilePath);
                if (!DataTableGenerator.CheckRawData(dataTableProcessor, dataTableName))
                {
                    Debug.LogError(GFUtility.Text.Format("Check raw data failure. DataTableName='{0}'", dataTableName));
                    break;
                }

                if(genBytesFile)
                    DataTableGenerator.GenerateDataFile(dataTableProcessor, dataTableName);

                DataTableGenerator.GenerateCodeFile(dataTableProcessor, dataTableName);
            }
            AssetDatabase.Refresh();
        }
    }
}
